import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_utils/date_time_util.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';
import '../../../hj_model/mine/credit_record_model.dart';
import '../../../tools_base/global_store/store.dart';
import 'integral_exchange_vip_view.dart';
import 'integral_record_logic.dart';

class ExchangeVipPage extends StatefulWidget {
  const ExchangeVipPage({super.key});

  @override
  State<ExchangeVipPage> createState() => _ExchangeVipPageState();
}

class _ExchangeVipPageState extends State<ExchangeVipPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('会员兑换')),
      body: Stack(
        children: [
          // 背景图
          Positioned.fill(
            child: Image.asset(
              'exchange_bg.webp'.mineImgPath, // 替换成你的图片路径
              fit: BoxFit.cover,
            ),
          ),
          // 背景图
          GetBuilder<IntegralRecordLogic>(
            init: IntegralRecordLogic(),
            builder: (_) => Padding(
                padding: EdgeInsets.symmetric(vertical: 28, horizontal: 16),
                child: Column(
                  children: [
                    Expanded(
                      child: pullYsRefresh(
                        onInit: (controller) => _.refreshController = controller,
                        onRefresh: (controller) => _.loadData(),
                        onLoading: (controller) => _.loadMoreData(),
                        child: CustomScrollView(
                          keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
                          slivers: <Widget>[
                            SliverToBoxAdapter(
                              child: _buildHeader(_),
                            ),
                            SliverToBoxAdapter(
                              child: IntegralExchangeVipView(),
                            ),
                            SliverToBoxAdapter(
                              child: _buildContent(_),
                            ),
                            ...[
                              _buildHistoryTable(_),
                            ]
                          ],
                        ),
                      ),
                    ),
                  ],
                )),
          )
        ],
      ),
    );
  }

  Widget _buildHistoryTable(IntegralRecordLogic logic) {
    if (logic.isLoadingHistoryData) {
      return SliverToBoxAdapter(child: LoadingCenterWidget());
    } else if (logic.groupList.isEmpty) {
      return SliverToBoxAdapter(
        child: CErrorWidget(errorMsg: "暂无兑换记录"),
      );
    } else {
      return SliverList.separated(
        itemBuilder: (context, index) {
          return _buildListItem(logic.groupList[index]);
        },
        itemCount: logic.groupList.length,
        separatorBuilder: (context, index) {
          return Divider(
            height: 1,
            color: Colors.black87.withOpacity(0.1),
          );
        },
      );
    }
  }

  _buildHeader(IntegralRecordLogic _){
    return  Container(
        margin: EdgeInsets.fromLTRB(0, 0, 0, 15),
        width: double.infinity,
        height:120,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(16),
            border: Border.all(color: Color(0xffE1351F), width: 2)
        ),
        child: Column(
          children: [
            16.sizeBoxH,
            Text(
              "当前积分",
              style: TextStyle(
                color: Colors.white.withOpacity(.9),
                fontWeight: FontWeight.w500,
                fontSize: 16,
              ),
            ),
            10.sizeBoxH,
            Consumer<GlobalStore>(
              builder: (_, store, __) => Text(
                '${store.wallet?.integral ?? 0}',
                style: TextStyle(color: Color(0xffE1351F), fontSize: 36, fontWeight: FontWeight.bold),
              ),
            ),
          ],
        ));
  }

  _buildContent(IntegralRecordLogic _) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "兑换记录",
          style: TextStyle(
            color: Colors.white.withOpacity(.9),
            fontWeight: FontWeight.w600,
            fontSize: 18,
          ),
        ),
        12.sizeBoxH,
        Row(
          children: [
            Expanded(child: _buildSectionItem('兑换方式')),
            5.sizeBoxW,
            Expanded(child: _buildSectionItem('兑换类型')),
            5.sizeBoxW,
            Expanded(child: _buildSectionItem('兑换时间')),
          ],
        ),
      ],
    );
  }

  _buildSectionItem(String title) {
    return Container(
      alignment: Alignment.center,
      height: 42,
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(.05),
      ),
      child: Text(
        title,
        style: TextStyle(
          color: Colors.white.withOpacity(.9),
          fontWeight: FontWeight.w400,
          fontSize: 14,
        ),
      ),
    );
  }

  Widget _buildListItem(CreditRecordModel item) {
    String desc = "";
    if(item.desc?.isNotEmpty == true){
      desc = item.desc??"-";
      List? list = desc.split("-");
      if(list.isNotEmpty == true&&list.length >1){
        desc = list[1];
      }
    }
    return Container(
      height: 44,
      child: Row(
        children: [
          Flexible(
            child: Container(
              height: 44,
              alignment: Alignment.center,
              child: Text(
                item.name ?? '',
                style: TextStyle(color: Colors.white, fontSize: 12),
              ),
            ),
          ),
          Flexible(
            child: Container(
              height: 44,
              alignment: Alignment.center,
              child: Text(
                desc,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(color:  Color(0xFF656565), fontSize: 12),
              ),
            ),
          ),
          Flexible(
            child: Container(
              height: 44,
              alignment: Alignment.center,
              child: Text(
                '${DateTimeUtil.utcTurnYear(item.createdAt)}',
                style: TextStyle(color: Color(0xFF656565), fontSize: 12),
              ),
            ),
          ),
        ],
      ),
    );
  }

}
